#include "userpage.h"
#include "ui_userpage.h"

userpage::userpage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::userpage)
{
    ui->setupUi(this);

    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //单击选择行


    connect(ui->tableWidget,&QTableWidget::cellClicked,[&](int row,int ){
        ui->lineEdit_name->setText(ui->tableWidget->item(row,0)->text());
        ui->lineEdit_sex->setText(ui->tableWidget->item(row,1)->text());
        ui->lineEdit_number->setText(ui->tableWidget->item(row,2)->text());
        ui->lineEdit_carid->setText(ui->tableWidget->item(row,3)->text());
        ui->lineEdit_stallid->setText(ui->tableWidget->item(row,4)->text());
    });
}

userpage::~userpage()
{
    delete ui;
}

void userpage::on_btn_query_clicked()
{
    Qurey();
}
void userpage::Qurey()
{
    int row = 0;
    ui->tableWidget->setRowCount(0);

    QSqlQuery query;
    if(query.exec("SELECT * FROM user"))
    {
        QSqlRecord record = query.record();
        ui->tableWidget->setColumnCount(record.count());
        ui->tableWidget->setHorizontalHeaderLabels({"名字","性别","电话号码","车牌号","车位号"});

        while(query.next())
        {
            ui->tableWidget->insertRow(row);
            for(int var = 0;var<record.count();++var)
            {
                ui->tableWidget->setItem(row,var,new QTableWidgetItem(query.value(var).toString()));
            }
            row++;
        }
    }
    else
    {
        qDebug() << "查询失败：" << query.lastError().text();
    }

}


void userpage::on_btn_add_clicked()
{
    QSqlQuery query;
    query.prepare("INSERT INTO user(name,sex,number,carid,stallid)VALUES(:value1,:value2,:value3,:value4,:value5)");
    query.bindValue(":value1",ui->lineEdit_name->text());
    query.bindValue(":value2",ui->lineEdit_sex->text());
    query.bindValue(":value3",ui->lineEdit_number->text().toInt());
    query.bindValue(":value4",ui->lineEdit_carid->text());
    query.bindValue(":value5",ui->lineEdit_stallid->text().toInt());

    if(query.exec())
    {
        Qurey();
    }
    else
    {
        qDebug() << "插入失败："<< query.lastError().text();
    }

    //更新车位类型
    QSqlQuery updatequery;
    int stallId = ui->lineEdit_stallid->text().toInt();
    updatequery.prepare("UPDATE stall SET type = 1 WHERE stallid=:stallid");
    updatequery.bindValue(":stallid",stallId);
    if(updatequery.exec())
    {
        Qurey();
    }
    else
    {
        qDebug() << "插入失败："<< updatequery.lastError().text();
    }
}

void userpage::on_btn_del_clicked()
{
    QSqlQuery query;
    query.prepare("DELETE FROM user WHERE name = :value1");
    query.bindValue(":value1",ui->lineEdit_name->text());

    if(query.exec())
    {
       Qurey();
    }
    else
    {
        qDebug() << "删除失败："<< query.lastError().text();
    }
    //更新车位类型
    QSqlQuery updatequery;
    int stallId = ui->lineEdit_stallid->text().toInt();
    updatequery.prepare("UPDATE stall SET type = 0  WHERE stallid=:stallid");
    updatequery.bindValue(":stallid",stallId);
    if(updatequery.exec())
    {
        Qurey();
    }
    else
    {
        qDebug() << "插入失败："<< updatequery.lastError().text();
    }

}

void userpage::on_btn_update_clicked()
{
    int stallId;
    //删除固定车位
    QSqlQuery updatequery;
    updatequery.prepare("SELECT stallid from user WHERE carid=:carid");
    updatequery.bindValue(":carid",ui->lineEdit_carid->text());
    if(updatequery.exec())
    {
        Qurey();
    }
    else
    {
        qDebug() << "插入失败："<< updatequery.lastError().text();
    }
    updatequery.next();
    stallId = updatequery.value(0).toInt();
    //更新表stall里面stallid对应的type

    qDebug()<< "更新表stall里面stallid对应的type" <<stallId;

    updatequery.prepare("UPDATE stall SET type = 0  WHERE stallid=:stallid");
    updatequery.bindValue(":stallid",stallId);
    if(updatequery.exec())
    {
        Qurey();
    }
    else
    {
        qDebug() << "插入失败："<< updatequery.lastError().text();
    }
    stallId = ui->lineEdit_stallid->text().toInt();
    updatequery.prepare("UPDATE stall SET type = 1  WHERE stallid=:stallid");
    updatequery.bindValue(":stallid",stallId);
    if(updatequery.exec())
    {
        Qurey();
    }
    else
    {
        qDebug() << "插入失败："<< updatequery.lastError().text();
    }
    //更新user表
    QSqlQuery query;
    query.prepare("UPDATE user SET  sex=:value2, number=:value3,carid=:value4,stallid=:value5 WHERE name=:value1");
    query.bindValue(":value1",ui->lineEdit_name->text());
    query.bindValue(":value2",ui->lineEdit_sex->text());
    query.bindValue(":value3",ui->lineEdit_number->text().toInt());
    query.bindValue(":value4",ui->lineEdit_carid->text());
    query.bindValue(":value5",ui->lineEdit_stallid->text().toInt());

    if (query.exec()) {
           Qurey();
        } else {
            qDebug() << "更新失败: " << query.lastError().text();
    }
}
